package friendShips;
import java.util.Iterator;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;



import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


@Path("/allFriends/{username}")
/**
 * This class is in charge of getting the wanted email and putting out all of the friends that are online
 * and sending back the locations so to the client so he will be able to display them on the map
 */
public class AllFriends {
@GET
@Produces("text/html")
public String getOut(@PathParam("username") String userName )
{
	SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); // open the session with the D.B
	Session session = sessionFactory.openSession();
	
	FriendManager manager = new FriendManager(session); // create manager for the friends
	
	Query query = session.createQuery("from FriendShip friendShip where friendShip.myEmail = :newEmail");
	query.setParameter("newEmail", userName); // get all the friends
	
	List <FriendShip> list = query.list(); 
	Iterator<FriendShip> it=list.iterator();
	
	
	
   String giver="";
   while (it.hasNext())
   {
	   Query query2 = session.createQuery("from User user where user.email = :newEmail"); // get the user from the D.B
	   String newMAil=it.next().getFriendsEmail();
	   query2.setParameter("newEmail",newMAil); // get the friends from D.B
	   List <User> list2 = query2.list();
	   Iterator<User> it2=list2.iterator(); // check if EMAIL exists
	   
	   User tempUser= new User();
	   tempUser=it2.next();
	   if (tempUser.isOnline()==true) // if the user is online
		   giver+=tempUser.getLocation()+","+tempUser.getEmail()+"\n";
		 
	  
	   
   }

	
	
	return giver;
	
}
}
